home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmcont.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  5.3 KB  |  120 lines

  1. // CmCont.h
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Abstract container definition.
  7. // -----------------------------------------------------------------
  8.  
  9. #ifndef _CMCONT_H
  10. #define _CMCONT_H
  11.  
  12. #include <cm/include/cmiter.h>
  13.  
  14. typedef Bool (*CmQueryFunc)(CmObject*, void*);          // Query function type.
  15. typedef Bool (CmObject::*CmQueryMemFunc)(void*);        // Query member function.
  16.  
  17. class CmContainer : public CmObject {                   // Container definition.
  18. public:
  19.   CmContainer() : _size(0), _ownsObjects(TRUE) {}       // Constructor.
  20.  
  21.   virtual ~CmContainer() {}                             // Virtual destructor.
  22.  
  23.   virtual CmObject*   operator[] (int) const;           // Get object by index.
  24.   virtual unsigned    size       () const;              // Return size.
  25.   virtual Bool        add        (CmObject*) = 0;       // Add object.
  26.   virtual Bool        remove     (CmObject*) = 0;       // Remove equal object.
  27.   virtual CmObject*   lookup     (CmObject*) const = 0; // Find equal object.
  28.   virtual Bool        contains   (CmObject*) const = 0; // Container has object?
  29.   virtual unsigned    occurrences(CmObject*) const = 0; // Count occurrences.
  30.   virtual void        removeAll  () = 0;                // Remove all objects.
  31.   virtual Bool        isEmpty    () const;              // Is container empty?
  32.   virtual CmIterator* newIterator() const = 0;          // Get new iterator.
  33.  
  34.   CmContainer* lookupAll (CmObject*) const;             // Find all equal objs.
  35.   Bool         addAllFrom(CmContainer*);                // Combine containers.
  36.  
  37.   void ownsObjects(Bool);                               // Set ownership policy.
  38.   Bool ownsObjects() const;                             // Get ownership policy.
  39.  
  40.   void printOn(ostream&) const;                         // Print to stream.
  41.   Bool write  (CmReserveFile&) const;                   // Write to reserve.
  42.   Bool read   (CmReserveFile&);                         // Read from reserve.
  43.  
  44.   CMOBJECT_ABSTRACT(CmContainer, CmObject)              // Define object funcs.
  45.  
  46.   // Call the specified function for every object in the container
  47.   // until the end is reached or the function returns FALSE.
  48.   void forEach(CmQueryFunc,    void* = NULL);
  49.   void forEach(CmQueryMemFunc, void* = NULL);
  50.  
  51.   // Return the first object to satisfy the conditional test performed
  52.   // in the specified function.
  53.   CmObject* firstThat(CmQueryFunc,    void* = NULL);
  54.   CmObject* firstThat(CmQueryMemFunc, void* = NULL);
  55.  
  56.   // Call the specified function for every object in the container
  57.   // adding all objects for which the function returns TRUE to an
  58.   // output container.
  59.   CmContainer* query(CmQueryFunc,    void* = NULL);
  60.   CmContainer* query(CmQueryMemFunc, void* = NULL);
  61.  
  62. protected:
  63.   virtual void copy(const CmContainer&);                // Copy method.
  64.  
  65.   Bool     writeBase(CmReserveFile&) const;             // Write base container.
  66.   unsigned readBase (CmReserveFile&);                   // Read base container.
  67.  
  68.   unsigned _size;                                       // Current size.
  69.   Bool     _ownsObjects;                                // Ownership flag.
  70. };
  71.  
  72. // "size" returns the container size.
  73. inline unsigned CmContainer::size() const
  74. { return _size; }
  75.  
  76. // "isEmpty" checks if the container is empty or not.
  77. inline Bool CmContainer::isEmpty() const
  78. { return (_size == 0); }
  79.  
  80. // "ownsObjects" sets a new ownership policy for this container.
  81. inline void CmContainer::ownsObjects(Bool O)
  82. { _ownsObjects = O; }
  83.  
  84. // "ownsObjects" returns the current ownership policy for this container.
  85. inline Bool CmContainer::ownsObjects() const
  86. { return _ownsObjects; }
  87.  
  88. // "CmForEach" and "CmEndFor" define a foreach loop where the object
  89. // pointer and collection are passed in and the object pointer will
  90. // point to the next object in the collection for each loop iteration.
  91. //
  92. #define CmForEach(obj, cltn) {                           \
  93.         CmIterator *___cm_iterator = cltn.newIterator(); \
  94.         while (!___cm_iterator->done()) {                \
  95.           obj = ___cm_iterator->next();
  96. #define CmEndFor } delete ___cm_iterator; }
  97.  
  98. // "CmRepeat" and "CmUntil" define a pascal style repeat/until loop
  99. // where the object pointer and collection are passed in and the object
  100. // pointer will point to the next object in the collection for each loop
  101. // iteration.
  102. //
  103. #define CmRepeat(obj, cltn) {                            \
  104.         CmIterator *___cm_iterator = cltn.newIterator(); \
  105.         do {                                             \
  106.           obj = ___cm_iterator->next();
  107. #define CmUntil(condition) } while (!(condition)); }
  108.  
  109. // "CmDo" and "CmWhile" define a typical do/while loop where the object
  110. // pointer and collection are passed in and the object pointer will point
  111. // to the next object in the collection for each loop iteration.
  112. //
  113. #define CmDo(obj, cltn) {                                \
  114.         CmIterator *___cm_iterator = cltn.newIterator(); \
  115.         do {                                             \
  116.           obj = ___cm_iterator->next();
  117. #define CmWhile(condition) } while (condition); }
  118.  
  119. #endif
  120.